package fragmentparent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;
import com.wq.cycling.ui.MainActivity;

import java.util.ArrayList;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForRepairer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.Constant;
import util.MySharePerference;
import util.adapter.ShopAdapter;
import util.myview.springview.DefaultFooter;
import util.myview.springview.DefaultHeader;
import util.myview.springview.SpringView;


/**
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class Fragment_B extends Fragment {

    private View rootView, ll_pop;
    private ListView lv_repairlistview;
    private ShopAdapter adapter;
    private Intent intent;
    private Bundle bundle;
    private ArrayList<EntityForSimple> mDatas = new ArrayList<>();;
    private SpringView springView;
    private int pageindex = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_fragment_b, container, false);
        initView();
        initData(pageindex);
        return rootView;
    }

    public void initView() {
        rootView.findViewById(R.id.tv_select).setOnClickListener(listener);
        rootView.findViewById(R.id.iv_historychat).setOnClickListener(listener);
        rootView.findViewById(R.id.tv_soso).setOnClickListener(listener);
        ll_pop = rootView.findViewById(R.id.ll_pop);
        ll_pop.setOnClickListener(listener);
        lv_repairlistview = ((ListView) rootView.findViewById(R.id.lv_repairlistview));
        adapter = new ShopAdapter(getActivity(), mDatas);
        lv_repairlistview.setAdapter(adapter);
        lv_repairlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent(getActivity(), SonActivity.class);
                bundle = new Bundle();
                bundle.putInt("code", Constant.ShopParticulars);
                bundle.putSerializable("mData",mDatas.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        initSpringView();
    }
    /**
     * 初始化springview组件
     */
    private void  initSpringView(){
        springView = (SpringView) rootView.findViewById(R.id.springview);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageindex = 1;
                initData(pageindex);
            }

            @Override
            public void onLoadmore() {
                pageindex++;
                initData(pageindex);
            }
        });
        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
    }

    public void initData(final int pageinedx) {
        ApiUtil.getApiService().repairerList(DemoApplication.getToken(), ((EditText) rootView.findViewById(R.id.et_search)).getText().toString(),MySharePerference.getShardPerferience(getActivity(),"LNG","0"),MySharePerference.getShardPerferience(getActivity(),"LAT","0"), pageinedx+"", "30").enqueue(new Callback<MessageForRepairer>() {
            @Override
            public void onResponse(Call<MessageForRepairer> call, Response<MessageForRepairer> response) {
                try {
                    MessageForRepairer entity = response.body();
                    if (entity.getCode().equals("10008")) {
                        springView.onFinishFreshAndLoad();
                        if(pageinedx ==1){
                            mDatas.clear();
                        }
                        mDatas.addAll(entity.getData().getRepaireres());
                        adapter.notifyDataSetChanged();
                    }else{
                        Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<MessageForRepairer> call, Throwable t) {
                Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
            }
        });
    }


    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_historychat:
                    // Toast.makeText(getActivity(),"聊天记录",Toast.LENGTH_LONG).show();
                    startActivity(new Intent(getActivity(), MainActivity.class));
                    break;
                case R.id.tv_select:
                    popShow();
                    break;
                case R.id.tv_select1:
                    popHide();
                    break;
                case R.id.tv_select2:
                    popHide();
                    break;
                case R.id.tv_select3:
                    popHide();
                    break;
                case R.id.ll_pop:
                    popHide();
                    break;
                case R.id.tv_soso:
                    pageindex = 1;
                    initData(pageindex);
                    break;
                default:
                    break;
            }
        }
    };

    public void popShow() {
        ll_pop.setVisibility(View.VISIBLE);
        AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
        animation.setDuration(300);
        ll_pop.setAnimation(animation);
    }

    public void popHide() {
        ll_pop.setVisibility(View.GONE);
        AlphaAnimation animation = new AlphaAnimation(1.0f, 0.1f);
        animation.setDuration(300);
        ll_pop.setAnimation(animation);
    }

    public static Fragment_B newInstance() {
        Fragment_B fragment = new Fragment_B();
        return fragment;
    }

}
